home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Tutorial / Cookbook / 27.Bounce / BounceView.m < prev    next >
Text File  |  1995-06-12  |  2KB  |  82 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "BounceView.h"
  5. #import <appkit/Form.h>
  6. #import <dpsclient/wraps.h>
  7. #import "ball.h"
  8. #import <appkit/Application.h>
  9.  
  10.  
  11. @implementation BounceView
  12.  
  13. void update (teNum, now, myself)
  14. DPSTimedEntry teNum;
  15. double now;
  16. id myself;
  17. {
  18.     [myself doTimedEntry];        
  19. }
  20.  
  21. +newFrame:(const NXRect *)tF {
  22.     self = [super newFrame:tF];
  23.     ballRadius = 8.0;
  24.     ballDiameter = ballRadius*2.0;
  25.     x = bounds.size.width/2.0;
  26.     y = bounds.size.height/2.0;
  27.     speed = 3.0; /* default speed in pixels per 1/10 second */
  28.     Xdirection = Ydirection = 1.0; /* +1 is right to left, bottom to top */
  29.     return self;
  30. }
  31.  
  32. - start:sender
  33. {
  34.     clockTE = DPSAddTimedEntry(0.1, &update, self, NX_MODALRESPTHRESHOLD);
  35.     return self;
  36. }
  37.  
  38. - stop:sender
  39. {
  40.     DPSRemoveTimedEntry (clockTE);
  41.     return self;
  42. }
  43.  
  44. - doTimedEntry
  45. {
  46.     static float localCounter;
  47.     x += speed*Xdirection;
  48.     y += speed*Ydirection;
  49.     if (x > bounds.size.width-ballRadius) Xdirection = -1.0;
  50.     if (x < ballRadius) Xdirection = 1.0;
  51.     if (y > bounds.size.height-ballRadius) Ydirection = -1.0;
  52.     if (y < ballRadius) Ydirection = 1.0;
  53.     [self display];
  54.     return self;
  55. }
  56.  
  57. // Good programming practice. Free what you create!
  58. - free
  59. {
  60.     [self stop:self];
  61.     return [super free];
  62. }
  63.  
  64. - speed:sender
  65. {
  66.     speed = [sender floatValue];
  67.     [self display];
  68.     return self;
  69. }
  70.  
  71.  
  72. - drawSelf:(NXRect*)r :(int)c
  73. {
  74.     NXEraseRect(&bounds);
  75.     // PSsetgray(NX_BLACK);
  76.     doBall(x, y, ballRadius);
  77.     return self;
  78. }
  79.  
  80.  
  81. @end
  82.